<template>
  <div class="container" v-if="!isLoading">
    <div class="title">眼动追踪测评项目</div>
    <div class="task-container">
      <div class="task-item" @click="select(1)" :class="{ active: isActive[1] }">
        <img src="../../../assets/img/homeIconFocus.png" />
        <p>AntiSaccade</p>
        <p>反眼跳任务</p>
      </div>
      <div class="task-item" @click="select(2)" :class="{ active: isActive[2] }">
        <img src="../../../assets/img/homeIconFocus.png" />
        <p>VPC</p>
        <p>视觉配对比较任务</p>
      </div>
      <div class="task-item" @click="select(3)" :class="{ active: isActive[3] }">
        <img src="../../../assets/img/homeIconFocus.png" />
        <p>VisMET</p>
        <p>视空间记忆眼动追踪任务</p>
      </div>
    </div>
    <div class="button-container">
      <el-button @click="confirm" type="primary">确认</el-button>
      <el-button @click="exit" type="danger">退出</el-button>
    </div>
  </div>
  <div class="loading" v-else="isLoading">
    <div class="loading-container">
      <div class="loading-dots">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { sendData } from "@/api/patient/index";
import { ElMessage } from 'element-plus'
const isLoading = ref(false)
const params = reactive({
  userName: '',
  sex: '',
  age: '',
  options: []
})
const router = useRouter();
const route = useRoute()
const { userName, sex, age, patientId } = route.query
params.userName = userName as string
params.sex = sex
params.age = age
params.patientId = patientId
let set = new Set()
let selectedData = reactive([])
const select = (value: any) => {
  isActive[value] = !isActive[value]
  if (set.has(value)) {
    set.delete(value)
    selectedData = [...set]
  } else {
    set.add(value)
    selectedData = [...set]
  }
}
const isActive = reactive({
  1: false,
  2: false,
  3: false
})
const confirm = () => {
  selectedData = [...set]
  if (selectedData.length === 0) {
    ElMessage({
      type: 'warning',
      message: '请选择至少一项测评任务'
    })
    return
  } else {
    params.options = selectedData
    isLoading.value = true
    // const loading = ElLoading.service()
    sendData(params).then(res => {
      if (res.data.result.taskId == 'error') {
        ElMessage({
          type: 'error',
          message: '程序执行失败，请重新开始执行或联系系统管理员'
        })
        router.push({
          path: '/patientManage'
        })
      } else {
        //眼动记录列表
        router.push({
          path: '/eyeTrackingRecord',
          // query: {
          //     taskId: res.data.result.taskId,
          //     pdfName:res.data.result.pdfName,
          //     flag:0
          // }
        })
      }
      // isLoading.value = false
    })
    // loading.close()
  }
}
const exit = () => {
  router.go(-1)
}
</script>
<style lang="scss" scoped>
.container {
  text-align: center;
  padding-top: 5%;

  .title {
    font-size: 40px;
    font-weight: 700;
    margin-bottom: 10%;
  }

  .task-container {
    display: flex;
    justify-content: space-around;
    color: #4c5894;
    font-size: 24px;
    padding-left: 8%;
    padding-right: 8%;

    .task-item {
      background-color: #F7F7F7;
      margin-bottom: 100px;
      width: 30%;
      cursor: pointer;
      padding-bottom: 20px;

      p {
        margin-top: 20px;
      }
    }

    .task-item:hover {
      background-color: #ecf1ff;
    }

    .active {
      background-color: #ecf1ff;
    }
  }

  .el-button {
    font-size: 28px;
    height: 64px;
    width: 200px;
    border-radius: 5px;
  }

  .el-button:nth-child(1) {
    /* background-color: #0056b3; */
    margin-right: 100px;
  }

  .el-button:nth-child(2) {
    /* background-color: #cb4444; */
  }
}

.loading {
  text-align: center;
  padding-top: 20%;
  font-size: 48px;
  font-weight: 700;

}

// loading样式
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  // height: 100vh;
  background-color: #fff;
}

.loading-dots {
  position: relative;
  width: 80px;
  height: 80px;
}

.loading-dots div {
  position: absolute;
  width: 16px;
  height: 16px;
  background-color: #00acfd;
  border-radius: 50%;
  animation: loading 1.2s linear infinite;
}

@keyframes loading {
  0% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

.loading-dots div:nth-child(1) {
  top: 0;
  left: 50%;
  margin-left: -8px;
  animation-delay: -1.1s;
}

.loading-dots div:nth-child(2) {
  top: 14%;
  left: 85%;
  margin-left: -8px;
  animation-delay: -1s;
}

.loading-dots div:nth-child(3) {
  top: 50%;
  left: 100%;
  margin-left: -8px;
  animation-delay: -0.9s;
}

.loading-dots div:nth-child(4) {
  top: 85%;
  left: 85%;
  margin-left: -8px;
  animation-delay: -0.8s;
}

.loading-dots div:nth-child(5) {
  top: 100%;
  left: 50%;
  margin-left: -8px;
  animation-delay: -0.7s;
}

.loading-dots div:nth-child(6) {
  top: 85%;
  left: 14%;
  margin-left: -8px;
  animation-delay: -0.6s;
}

.loading-dots div:nth-child(7) {
  top: 50%;
  left: 0;
  margin-left: -8px;
  animation-delay: -0.5s;
}

.loading-dots div:nth-child(8) {
  top: 14%;
  left: 14%;
  margin-left: -8px;
  animation-delay: -0.4s;
}
</style>